<?php

namespace app\admin\controller\prize;

use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Draw extends Backend
{
    
    /**
     * PrizeDraw模型对象
     * @var \app\admin\model\PrizeDraw
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段 不加表前缀
     */
    protected $searchFields = 'id,prize.name,user.username';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\PrizeDraw;

        //未领奖  已领奖  已弃奖
        $typeList = [
            'noprize' => '未领奖',
            'received' => '已领奖',
            'giveup'  => '已弃奖',
        ];

        $this->view->assign("typeList", $typeList);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            $map = [
                'is_opened' => 1,
                'is_win'    => 1,
            ];

            $status = $this->request->request("type");
            switch ($status){
                case 'noprize':
                    //未领奖
                    $map['prize_draw.status'] = 0;
                    break;
                case 'received':
                    //已领奖
                    $map['prize_draw.status'] = 1;
                    //已付款
                    $map['order.pay_state'] = 1;
                    //已发货
                    //$map['order.ship_state'] = ['neq',0];
                    break;
                case 'giveup':
                    //已弃奖
                    $map['prize_draw.status'] = 2;
                    break;
                default:
                    break;

            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['user','prize','goods','order'])
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['user','prize','goods','order'])
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $statusDict = [
                0 => '未领奖',
                1 => '已领奖',
                2 => '已弃奖',
            ];
            foreach ($list as $k=>$v){
                //返回领奖信息
                $tips = '';
                if ($v['status'] == 0) {
                    if ($v['order_sn']){
                        $tips = '待付款';
                    }else{
                        $tips = '未领奖';
                    }
                }elseif ($v['status'] == 1){
                    if ($v['order']['state'] == 1){
                        if ($v['order']['pay_state'] == 1){
                            if ($v['order']['ship_state'] == 1){
                                $tips = '待收货';
                            }else{
                                $tips = '待发货';
                            }
                        }
                    }elseif ($v['order']['state'] == 2){
                        $tips = '已完成';
                    }elseif ($v['order']['state'] == 3){
                        $tips = '已取消';
                    }
                }elseif ($v['status'] == 2){
                    $tips = '已弃奖';
                }

                $list[$k]['status'] = $tips;
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function show($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $statusDict = [
            0 => '未领奖',
            1 => '已领奖',
            2 => '已弃奖',
        ];
        $row['status'] = isset($statusDict[$row['status']]) ? $statusDict[$row['status']] : '-';

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
